package com.zmc.admin.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.admin.infrastructure.constant.UserConstants;
import com.zmc.admin.infrastructure.dao.SysDictTypeDao;
import com.zmc.admin.infrastructure.bean.query.SysDictTypeQuery;
import com.zmc.admin.infrastructure.service.SysDictDataService;
import com.zmc.common.exception.base.WebDisplayException;
import com.zmc.common.utils.DictUtils;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zmc.admin.infrastructure.entity.SysDictTypeEntity;
import com.zmc.admin.infrastructure.service.SysDictTypeService;
import org.springframework.util.CollectionUtils;


/**
 * @author zmc
 */
@Service("sysDictTypeService")
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeDao, SysDictTypeEntity> implements SysDictTypeService {

    public static Logger log = LoggerFactory.getLogger(SysDictTypeServiceImpl.class);

    @Autowired
    private SysDictDataService dictDataService;

    @Override
    public PageUtils queryPage(SysDictTypeQuery query) {
        Page<SysDictTypeEntity> page = baseMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), getWrapper(query));
        return new PageUtils(page);
    }

    @Override
    public boolean checkDictTypeUnique(SysDictTypeEntity dict) {
        LambdaQueryWrapper<SysDictTypeEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDictTypeEntity::getDictType, dict.getDictType());
        List<SysDictTypeEntity> list = list(wrapper);
        if (!CollectionUtils.isEmpty(list) && list.size() > 1) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public List<SysDictTypeEntity> selectDictTypeList(SysDictTypeQuery query) {
        return list(getWrapper(query));
    }

    @Override
    public void deleteDictTypeById(Long dictId) {
        SysDictTypeEntity dictType = getById(dictId);
        if (dictDataService.countDictDataByType(dictType.getDictType()) > 0) {
            log.error(String.format("%1$s已分配,不能删除", dictType.getDictName()));
            throw new WebDisplayException("字典类型已分配,不能删除");
        }
        removeById(dictId);
        DictUtils.removeDictCache(dictType.getDictType());
    }

    public void loadingDictCache() {
    }

    @Override
    public void clearDictCache() {
        DictUtils.clearDictCache();
    }

    /**
     * 重置字典缓存数据
     */
    @Override
    public void resetDictCache() {
        clearDictCache();
        loadingDictCache();
    }


    protected LambdaQueryWrapper<SysDictTypeEntity> getWrapper(SysDictTypeQuery query) {
        LambdaQueryWrapper<SysDictTypeEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(query.getDictName()), SysDictTypeEntity::getDictName, query.getDictName())
                .eq(StringUtils.isNotEmpty(query.getDictType()), SysDictTypeEntity::getDictType, query.getDictType())
                .eq(StringUtils.isNotEmpty(query.getStatus()), SysDictTypeEntity::getStatus, query.getStatus())
                .ge(StringUtils.isNotEmpty(query.getBeginTime()), SysDictTypeEntity::getCreateTime, query.getBeginTime())
                .le(StringUtils.isNotEmpty(query.getEndTime()), SysDictTypeEntity::getCreateTime, query.getEndTime());
        return wrapper;
    }

}